package com.sunspring.zdcall.util;

import java.text.SimpleDateFormat;
import java.time.*;
import java.util.Date;

/**
 * @Author WuLei
 * @Date 2018/11/28  15:51
 */
public class TimeUtil {

    private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * date转localDateTime
     */
    public static LocalDateTime conDateToLocalDateTime(Date date) {
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zone);
    }

    /**
     * localDateTime转date
     */
    public static Date conLocalDateTimeToDate(LocalDateTime localDateTime) {
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        return Date.from(instant);
    }

    /**
     * 当天的起始时间 xxx00:00:00
     * @return Date
     */
    public static Date todayStartDate() {
        LocalDateTime localDateTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        return conLocalDateTimeToDate(localDateTime);
    }

    /**
     * 当天的结束时间 xxx23:59:59
     * @return Date
     */
    public static Date todayEndDate() {
        LocalDateTime localDateTime = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        return conLocalDateTimeToDate(localDateTime);
    }

    /**
     * 时间格式化
     */
    public static String format(Date time) {
        return sdf.format(time);
    }

}
